package com.zhixing.order.controller;

import com.zhixing.common.annotation.Driver;
import com.zhixing.common.annotation.Passenger;
import com.zhixing.common.domain.dto.RoutePlanningDTO;
import com.zhixing.common.utils.Res;
import com.zhixing.common.utils.UserContext;
import com.zhixing.order.domain.dto.OrderGrabInfoDTO;
import com.zhixing.order.domain.dto.OrderLocationDTO;
import com.zhixing.order.domain.vo.*;
import com.zhixing.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 订单接口
 */
@Slf4j
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 预估订单信息
     * @param routePlanningDTO 位置信息
     * @return OrderEstimateInfoVO 订单预估信息
     */
    @Passenger
    @GetMapping("/estimateOrderInfo")
    public Res<OrderEstimateInfoVO> estimateOrderInfo(@Validated RoutePlanningDTO routePlanningDTO) {
        log.info("订单服务调用，预估订单信息");
        return Res.ok(orderService.estimateOrderInfo(routePlanningDTO));
    }

    /**
     * 下单
     * @param orderLocationDTO 订单位置信息
     * @return Long 订单id
     */
    @Passenger
    @PostMapping("/submitOrder")
    public Res<Long> submitOrder(@Validated @RequestBody OrderLocationDTO orderLocationDTO) {
        Integer userId = UserContext.getUserInfo().getId();
        log.info("订单服务调用，下单，用户id：{}", userId);
        return Res.ok(orderService.submitOrder(userId, orderLocationDTO));
    }

    /**
     * 取消订单
     * @param orderId 订单id
     * @return 操作结果
     */
    @Passenger
    @PutMapping("/cancelOrder/{orderId}")
    public Res<Void> cancelOrder(@PathVariable Long orderId) {
        log.info("订单服务调用，取消订单，订单id：{}", orderId);
        orderService.cancelOrder(orderId);
        return Res.ok();
    }

    /**
     * 抢单
     * @param orderGrabInfoDTO 订单抢夺传输信息
     * @return 操作结果
     */
    @Driver
    @PutMapping("/grabOrder")
    public Res<Void> grabOrder(@Validated @RequestBody OrderGrabInfoDTO orderGrabInfoDTO) {
        log.info("订单服务调用，抢单，订单id：{}，司机id：{}", orderGrabInfoDTO.getId(), orderGrabInfoDTO.getDriverId());
        orderService.grabOrder(orderGrabInfoDTO);
        return Res.ok();
    }

    /**
     * 司机获取接单信息
     * @param orderId 订单id
     * @return DriverReceiveOrderVO 司机接单信息
     */
    @Driver
    @GetMapping("/driverReceiveOrder/{orderId}")
    public Res<DriverReceiveOrderVO> driverGetReceiveOrderInfo(@PathVariable Long orderId) {
        log.info("订单服务调用，司机获取接单信息，订单id：{}", orderId);
        return Res.ok(orderService.driverGetReceiveOrderInfo(orderId));
    }

    /**
     * 乘客获取接单信息
     * @param orderId 订单id
     * @return PassengerReceiveOrderVO 乘客接单信息
     */
    @Passenger
    @GetMapping("/passengerReceiveOrder/{orderId}")
    public Res<PassengerReceiveOrderVO> passengerGetReceiveOrderInfo(@PathVariable Long orderId) {
        log.info("订单服务调用，乘客获取接单信息，订单id：{}", orderId);
        return Res.ok(orderService.passengerGetReceiveOrderInfo(orderId));
    }

    /**
     * 开始服务
     * @param orderId 订单id
     * @param tailNumber 手机尾号
     * @return 操作结果
     */
    @Driver
    @PutMapping("/startService/{orderId}/{tailNumber}")
    public Res<Void> startService(@PathVariable Long orderId, @PathVariable String tailNumber) {
        log.info("订单服务调用，开始服务，订单id：{}", orderId);
        orderService.startService(orderId, tailNumber);
        return Res.ok();
    }

    /**
     * 获取订单开始信息
     * @param orderId 订单id
     * @return OrderStartInfoVO 订单开始信息
     */
    @GetMapping("/orderStartInfo/{orderId}")
    public Res<OrderStartInfoVO> getOrderStartInfo(@PathVariable Long orderId) {
        log.info("订单服务调用，获取订单开始信息，订单id：{}", orderId);
        return Res.ok(orderService.getOrderStartInfo(orderId));
    }

    /**
     * 到达终点
     * @param orderId 订单id
     * @return 操作结果
     */
    @Driver
    @PutMapping("/arrive/{orderId}")
    public Res<Void> arriveEnd(@PathVariable Long orderId) {
        log.info("订单服务调用，到达终点，订单id：{}", orderId);
        orderService.arriveEnd(orderId);
        return Res.ok();
    }

    /**
     * 获取订单支付信息
     * @param orderId 订单id
     * @return OrderPayInfoVO 订单支付信息
     */
    @Passenger
    @GetMapping("/orderPayInfo/{orderId}")
    public Res<OrderPayInfoVO> getOrderPayInfo(@PathVariable Long orderId) {
        log.info("订单服务调用，获取订单支付信息，订单id：{}", orderId);
        return Res.ok(orderService.getOrderPayInfo(orderId));
    }

    /**
     * 获取订单费用规则
     * @param orderId 订单id
     * @return FeeRuleVO 费用规则
     */
    @Passenger
    @GetMapping("/orderFeeRule/{orderId}")
    public Res<FeeRuleVO> getOrderFeeRule(@PathVariable Long orderId) {
        log.info("订单服务调用，获取订单费用规则，订单id：{}", orderId);
        return Res.ok(orderService.getOrderFeeRule(orderId));
    }

    /**
     * 支付订单
     * @param orderId 订单id
     * @param couponId 优惠券id
     * @return 操作结果
     */
    @Passenger
    @PutMapping("/payOrder")
    public Res<Void> payOrder(@RequestParam Long orderId, @RequestParam(required = false) Integer couponId) {
        Integer userId = UserContext.getUserInfo().getId();
        log.info("订单服务调用，支付订单，订单id：{}，用户id：{}", orderId, userId);
        orderService.payOrder(orderId, couponId, userId);
        return Res.ok();
    }
}
